package com.eeepay.eeepay_v2.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.ApplyforDevicesAdapter;
import com.eeepay.eeepay_v2.api.TerminalNoTypeDef;
import com.eeepay.eeepay_v2.fragment.ApplyforDevicesFragment;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：机具申请列表页
 * 1 待处理，2已处理，3全部
 * 作者：zhuangzeqin
 * 时间: 2017/7/19-15:52
 * 邮箱：zzq@eeepay.cn
 */
public class ApplyforDevicesActivity extends ABBaseActivity {
    private TabLayout tab_layout;
    private ViewPager viewpager;
//    private String[] titles = new String[]{"待处理", "已处理", "全部"};
    private List<Fragment> mFragmentTab;
    private ApplyforDevicesAdapter adapter;
    private TitleBar title_bar;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_applyfor_devices;
    }

    @Override
    protected void initWidget() {
        tab_layout = getViewById(R.id.tab_layout);
        title_bar = getViewById(R.id.title_bar);
        viewpager = getViewById(R.id.viewpager);
        initTab();
    }

    private void initTab() {
        mFragmentTab = new ArrayList<>();
        mFragmentTab.add(ApplyforDevicesFragment.getInstance(TerminalNoTypeDef.KEY_DCL));//待处理
        mFragmentTab.add(ApplyforDevicesFragment.getInstance(TerminalNoTypeDef.KEY_YCL));//已处理
        mFragmentTab.add(ApplyforDevicesFragment.getInstance(TerminalNoTypeDef.KEY_ALL));//全部

        adapter = new ApplyforDevicesAdapter(getSupportFragmentManager());
        adapter.setListFragments(mFragmentTab);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(2);
//        viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));
        tab_layout.setupWithViewPager(viewpager);//关联viewPage
        tab_layout.setTabGravity(TabLayout.GRAVITY_FILL);
        tab_layout.setTabMode(TabLayout.MODE_FIXED);


    }

    @Override
    protected void eventOnClick() {
        title_bar.setLeftOnClickListener(new TitleBar.LeftBtnOnClickListener() {
            @Override
            public void onLeftClick(View view) {
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            setResult(RESULT_OK);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
